using SmartHRMS.CustomExceptions.Infrastructure;
using System;
using System.Text;

namespace SmartHRMS.Model
{
    public class Address
    {
        public Guid AddressId { get; set; }

        public string Country { get; set; }

        public string Street { get; set; }

        public string City { get; set; }

        public string State { get; set; }

        public string PostCode { get; set; }

        public string CellPhone { get; set; }

        public string HomePhone { get; set; }

        public string OfficePhone { get; set; }

        public string Fax { get; set; }

        public string Email { get; set; }

        public string Website { get; set; }
		
        public Address()
        {
            // Default Constructor required for NHibernate
        }
		
        public Address(Guid addressId, string street, string city, string county,string state, string postCode,
                       string cellPhone, string homePhone, string officePhone, string fax, string email, string website)
        {
            this.AddressId = addressId;
            this.Street = street;
            this.City = city;
            this.State = state;
            this.Country = county;
            this.PostCode = postCode;
            this.CellPhone = cellPhone;
            this.HomePhone = homePhone;
            this.OfficePhone = officePhone;
            this.Fax = fax;
            this.Email = email;
            this.Website = website;			
            validate();
        }
		
        protected virtual void validate()
        {
            StringBuilder sbBrokenRules = new StringBuilder();
			
            if (string.IsNullOrEmpty(this.Street.Trim()))
            {
                sbBrokenRules.AppendLine("Street is a required value");
            }
            else
            {
                if (this.Street.Length > 50)
                {
                    sbBrokenRules.AppendLine("Street should be 50 chars or less");
                }
            }


            if (string.IsNullOrEmpty(this.State.Trim()))
            {
                sbBrokenRules.AppendLine("State is a required value");
            }
            else
            {
                if (this.State.Length > 50)
                {
                    sbBrokenRules.AppendLine("State should be 50 chars or less");
                }
            }

			
            if (string.IsNullOrEmpty(this.City.Trim()))
            {
                sbBrokenRules.AppendLine("City is a required value");
            }
            else
            {
                if (this.City.Length > 50)
                {
                    sbBrokenRules.AppendLine("City should be 50 chars or less");
                }
            }
			
            if (string.IsNullOrEmpty(this.Country.Trim()))
            {
                sbBrokenRules.AppendLine("County is a required value");
            }
            else
            {
                if (this.Country.Length > 50)
                {
                    sbBrokenRules.AppendLine("County should be 50 chars or less");
                }
            }
			
            if (string.IsNullOrEmpty(this.PostCode.Trim()))
            {
                sbBrokenRules.AppendLine("PostCode is a required value");
            }
            else
            {
                if (this.PostCode.Length > 50)
                {
                    sbBrokenRules.AppendLine("PostCode should be 50 chars or less");
                }
            }

            if (!string.IsNullOrEmpty(this.CellPhone.Trim()))
            {
                if (this.CellPhone.Length > 15)
                {
                    sbBrokenRules.AppendLine("Cell Phone should be 15 chars or less");
                }
                //additiona validation goes here
            }

            if (!string.IsNullOrEmpty(this.HomePhone.Trim()))
            {
                if (this.HomePhone.Length > 15)
                {
                    sbBrokenRules.AppendLine("Home Phone should be 15 chars or less");
                }
                //additiona validation goes here
            }

            if (!string.IsNullOrEmpty(this.OfficePhone.Trim()))
            {
                if (this.OfficePhone.Length > 15)
                {
                    sbBrokenRules.AppendLine("Office Phone should be 15 chars or less");
                }
                //additiona validation goes here
            }

            if (!string.IsNullOrEmpty(this.Fax.Trim()))
            {
                if (this.Fax.Length > 15)
                {
                    sbBrokenRules.AppendLine("Fax should be 15 chars or less");
                }
                //additiona validation goes here
            }

            if (!string.IsNullOrEmpty(this.Email.Trim()))
            {
                if (this.Email.Length > 250)
                {
                    sbBrokenRules.AppendLine("Email should be 250 chars or less");
                }
                //additiona validation goes here
            }

            if (!string.IsNullOrEmpty(this.Website.Trim()))
            {
                if (this.Website.Length > 250)
                {
                    sbBrokenRules.AppendLine("Website should be 250 chars or less");
                }
            }
			
            if (! string.IsNullOrEmpty(sbBrokenRules.ToString()))
            {
                throw (new InvalidAddressException(string.Format("Broken rules on Address, please correct the following errors:{0}{1}", Environment.NewLine, sbBrokenRules.ToString())));
            }
        }
    }
}
